﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RapidTestMapMaker
{
    public class TileSet
    {
        private int tileWidth = 32;
        public int TileWidth
        { get { return tileWidth; } set { tileWidth = value; } }

        private int tileHeight = 32;
        public int TileHeight
        { get { return tileHeight; } set { tileHeight = value; } }

        public IList<Tile> Tiles
        {
            get
            {
                if (tilesRO == null)
                { tilesRO = tiles.AsReadOnly(); }
                return tilesRO;
            }
        }

        private List<Tile> tiles = new List<Tile>();
        private IList<Tile> tilesRO;

        public void AddTile(Tile tile)
        {
            if (tile != null && !tiles.Contains(tile))
            {
                tiles.Add(tile);
                tile.TileChanged += tile_TileChanged;
                if (ActiveTile == null)
                { ActiveTile = tile; }
                if (TileChanged != null)
                { TileChanged(tile, new EventArgs()); }
            }
        }

        public void RemoveTile(Tile tile)
        {
            if (tile != null && tiles.Contains(tile))
            {
                tiles.Remove(tile);
                tile.TileChanged -= tile_TileChanged;
                if (TileChanged != null)
                {
                    TileChanged(tile, new EventArgs());
                }
            }
        }

        void tile_TileChanged(object sender, EventArgs e)
        {
            if (TileChanged != null)
            {
                TileChanged(sender, e);
            }
        }

        public Tile ActiveTile
        { get; set; }

        public event EventHandler<EventArgs> TileChanged;
    }
}
